﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Benchmark_System {
    public partial class GUI : Form {

        private BenchmarkSystem bs;
        /// <summary>
        /// Initializes the GUI.
        /// </summary>
        public GUI() {
            bs = new BenchmarkSystem("System.txt");
            InitializeComponent();

        }
        /// <summary>
        /// Addbutton creates a job and runs the updatelistview method
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttAddJob_Click(object sender, EventArgs e) {
            CreateJob();
            updateListview();
        }
        /// <summary>
        /// Merges the different lists with jobs into one list.
        /// Runs the list through and adds Job to the listView.
        /// </summary>
        private void updateListview() {
            lock(listView1) {
            List<Job> all = new List<Job>();
            all.AddRange(bs.scheduler.ShortQueue);
            all.AddRange(bs.scheduler.LongQueue);
            all.AddRange(bs.scheduler.VeryLongQueue);
            all.AddRange(bs.RunningJobs);
            all.Sort((a,b) => b.owner.name.CompareTo(a.owner.name));

            listView1.Items.Clear();
            foreach (Job j in all) {
                int i = 0;
                ListViewItem lwi = new ListViewItem();
                lwi.SubItems.Add("");
                lwi.SubItems.Add("");
                lwi.SubItems.Add("");

                lwi.SubItems[0].Text = j.owner.name;
                lwi.SubItems[1].Text = j.Cpu + "";
                lwi.SubItems[2].Text = j.ExpectedRuntime + "";
                lwi.SubItems[3].Text = j.State + "";
                listView1.Items.Insert(i, lwi);
                i++;
            }
            
            }
        }
        /// <summary>
        /// Creates a job from the userinput.
        /// </summary>
        private void CreateJob() {

            if (int.Parse(textBoxCPU.Text) > 0 && int.Parse(textBoxRuntime.Text) > 0) {
                int cpu = int.Parse(textBoxCPU.Text);
                int runtime = int.Parse(textBoxRuntime.Text);
                String owner = textBoxOwner.Text;
                bs.Submit(new Job(cpu, runtime, Job.JobState.QUEUED, n => "Run and run"), new Owner(owner));
                textBoxCPU.Text = "";
                textBoxRuntime.Text = "";
                textBoxOwner.Text = "";

            }
        }
        /// <summary>
        /// Executes all job when user hits the Execute button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttExecute_Click(object sender, EventArgs e) {
            bs.ExecuteAll();
        }
        /// <summary>
        /// Updates the listview when user hits the Refresh button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttRefresh_Click(object sender, EventArgs e) {
            updateListview();
        }
    }
}
